package Server; /************************************************************************
 *  This class encaqpsulates a book sellar database containing ArrayLists
 *  of books, of customers and of order records. The constructor populates
 *  the arrays from files; adding an order or a customer also saves the
 *  ArrayList to the file.
 *
 *  Michael Brockway, March 2011
 ***********************************************************************/

import java.util.*;
import java.io.*;


public class BookStoreDB {
  private ArrayList<BookRec> books;  
  private ArrayList<CustRec> custs;  
  private ArrayList<OrderRec> orders;

  //Get Date 'now' in format dd/mm/yy. Uses java.util.Calendar.
  public static String getDate() {
    Calendar c = Calendar.getInstance();
    return String.format("%02d/%02d/%02d", c.get(Calendar.DAY_OF_MONTH),
      c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR)-2000);
  }

  //Constructor creates the 3 ArrayLists and populates from files.
  public BookStoreDB() {
    try { popBooks();  }
    catch(IOException ex) {
        ex.printStackTrace();
      System.out.println("Trouble reading books data file");
    }
    try { popCusts();  }
    catch(IOException ex) {
      System.out.println("Trouble reading customer records file");
    }
    try { popOrders();  }
    catch(IOException ex) {
      System.out.println("Trouble reading books order records file");
    }
  }

  //Three helper functions scan files using java.util.Scanner with
  //delimiter = \t (tab) or \n (new line), possibly with spaces before/after. 
  private void popBooks() throws IOException {
    books = new ArrayList<BookRec>();
    Reader  rdr = new BufferedReader(new FileReader("NuReadBookList.txt"));
    Scanner scnr = new Scanner(rdr).useDelimiter("\\s*[\\t\\n]\\s*");
    while(scnr.hasNextInt()) {
      books.add(new BookRec(scnr.nextInt(), scnr.next(),
        scnr.next(), scnr.next(), scnr.next(), scnr.next()));
    }
    scnr.close();
    rdr.close();
  }

  private void popCusts() throws IOException {
    custs = new ArrayList<CustRec>();
    Reader  rdr = new BufferedReader(new FileReader("NuReadCustomerList.txt"));
    Scanner scnr = new Scanner(rdr).useDelimiter("\\s*[\\t\\n]\\s*");
    while(scnr.hasNextInt()) {
      custs.add(new CustRec(scnr.nextInt(), scnr.next(), scnr.next()));
    }
    scnr.close();
    rdr.close();
  }

  private void popOrders() throws IOException {
    orders = new ArrayList<OrderRec>();
    Reader  rdr = new BufferedReader(new FileReader("NuReadOrderList.txt"));
    Scanner scnr = new Scanner(rdr).useDelimiter("\\s*[\\t\\n]\\s*");
    while(scnr.hasNextInt()) {
      orders.add(new OrderRec(scnr.nextInt(), scnr.nextInt(),
        scnr.next(), scnr.next(), scnr.next(), scnr.next()));
    }
    scnr.close();
    rdr.close();
  }

  //Need these for external access to the arrays eg for searching --
  public ArrayList<BookRec>  getBooks()  { return books; } 
  public ArrayList<CustRec>  getCusts()  { return custs; }  
  public ArrayList<OrderRec> getOrders() { return orders; }

  //Display book records
  public void showBooks() {
    System.out.println ("ID\tAuthor\t\tTitle\t\tCategory\tPrice\tAvailable");
    for (BookRec b: books)
      System.out.print(b.toString());
    System.out.printf("%d books in catalogue.\n", books.size());
  }

  //Display customer records
  public void showCusts() {
    System.out.println ("ID\tName\t\tAddress");
    for (CustRec c: custs)
      System.out.print(c.toString());
    System.out.printf("%d registered customers.\n", custs.size());
 }

  //Display order records
  public void showOrders() {
    System.out.println ("Book\tCust\tCharge\tDate\tStatus\tAvail");
    for (OrderRec o: orders)
      System.out.print(o.toString());
    System.out.printf("we have %d orders.\n", orders.size());
  }

  //Save customer records
  public void saveCusts() throws IOException {
    PrintWriter out = new PrintWriter(new FileWriter("NuReadCustomerList.txt"));
    for (CustRec c: custs){
        out.print(c.toString());
    }
    out.close();
  }

  //Save order records
  public void saveOrders() throws IOException {
    PrintWriter out = new PrintWriter(new FileWriter("NuReadOrderList.txt"));
    for (OrderRec o: orders)
      out.print(o.toString());
    out.close();
  }

  //Adds a new customer to array of customers and refreshes the file.
  void addCust (int id, String name, String addr) {
    custs.add(new CustRec(id, name, addr));
    try {
      saveCusts();
    } catch(IOException ex) {
      System.out.println ("Trouble saving customer records");
    }
  }

  //Adds a new order to array of orders and refreshes the file.
  void addOrder (int bk, int cs, 
      String money, String when, String stat, String av) {
    orders.add(new OrderRec(bk, cs, money, when, stat, av));
    try {
      saveOrders();
    } catch(IOException ex) {
      System.out.println ("Trouble saving order records");
    }
}

  //This main is purely for testing. Not used in "production".
  public static void main(String[] args) {
    String todaysDate = BookStoreDB.getDate();
    System.out.printf("The date today is %s\n", todaysDate);

    BookStoreDB db = new BookStoreDB();

    db.showBooks();
 
    db.showCusts();
 
    db.addCust(999, "Fred Nurg", "23 The Shacks, Junktown, JJ99 9JJ");
    db.showCusts();

    db.showOrders ();

    db.addOrder (1234, 7654, "12.34", todaysDate, "tbd", "Y");
    db.showOrders ();
  }
}
